import allure
from airtest.core.api import *
from common.airtest.airtest_base import AirtestBase
from common.log.log import log
from config.conf import BASE_DIR
from po.jinhua.applets_po.submit_order_page import SubmitOrderPage


class ShoppingCartPage:
    """
    购物车页面，相关功能
    """

    @allure.step('清空购物车')
    def clear_shopping_cart(self):
        """
        清空购物车
        """
        try:
            log.info('清空购物车')
            if not exists(Template(BASE_DIR + r"/element_picture/购物车/暂无商品.png")):
                AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/管理.png")
                AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/删除.png")
                if exists(Template(BASE_DIR + r"/element_picture/购物车/暂无商品.png")):
                    log.info('购物车清空完成！')
                    return True
                else:
                    log.info('购物车删除商品出现问题，未清空完！！')
                    return False
            else:
                log.info('购物车无商品不需要清空！')
        except Exception as e:
            log.error(f'进入分类，商品分类列表出现异常为：{e}')
            raise e

    @allure.step('购物车列表，收藏')
    def shopping_cart_collection(self):
        """购物车列表，收藏"""
        try:
            log.info('购物车列表，收藏商品')
            AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/管理.png")
            AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/收藏.png")
            sleep(1)
            log.info('收藏完成！')
        except Exception as e:
            log.error(f'购物车列表收藏商品出现异常为：{e}')
            raise e

    @allure.step('购物车列表，立即下单购买')
    def shopping_cart_buy(self):
        """购物车列表，立即下单购买"""
        try:
            log.info('购物车列表，立即下单购买')
            AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/立即下单按钮.png")
            return SubmitOrderPage()
        except Exception as e:
            log.error(f'购物车列表，立即下单购买出现异常为：{e}')
            raise e

    @allure.step('购物车列表，点击商品进入详情页')
    def shopping_cart_go_to_details_page(self):
        """购物车列表，点击商品进入详情页"""
        try:
            log.info('购物车列表，点击商品进入详情页')
            AirtestBase().click_5(BASE_DIR + r"/element_picture/购物车/￥.png")
            return SubmitOrderPage()
        except Exception as e:
            log.error(f'购物车列表，点击商品进入详情页出现异常为：{e}')
            raise e
